home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2609 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.6 KB

  1. Path: globe.indirect.com!snoopy
  2. From: chrism@xroads.com (Chris McCabe)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Doesn't work, Why EXACTLY?
  5. Date: 18 Jan 1996 18:32:25 GMT
  6. Organization: Internet Direct, Inc.
  7. Message-ID: <4dlsg9$7a_004@news.indirect.com>
  8. References: <30FA8289.7AD6@bangate.compaq.com>
  9. NNTP-Posting-Host: snoopy.infograph.com
  10. X-Newsreader: News Xpress Version 1.0 Beta #3
  11.  
  12.   The compiler searches the scope hierarchy until it finds the
  13. function name it is looking for.  It then looks for a best match
  14. within that scope ONLY.  Base class methods will not be considered.
  15.   In your case, it found func2 in class Y, then tried to match
  16. the X* to the Y* argument, which it can't do, so it gave the
  17. error.  If you had no func2 method in class Y, it would have worked
  18. fine because the compiler would have searched the scope of class X
  19. instead.
  20.   Whenever you overload a function name, you should consider
  21. overloading all instances of the function.  Otherwise they
  22. will be "hidden" and unusable in the overloading scope.
  23.   To fix your problem, simply add an inline function to class Y
  24. as follows:
  25.    inline void func2(X* x)  { X::func2(x); }
  26.  
  27.   This will fix the problem and does not incur any runtime overhead.
  28.  
  29.   Hope this helps,
  30.    Chris
  31.  
  32.  
  33. In article <30FA8289.7AD6@bangate.compaq.com>,
  34.    Saurabh Dixit <saurabhd@bangate.compaq.com> wrote:
  35. [stuff deleted...]
  36. >when I call func2 from a Derived object with a pointer
  37. >to an X object, i get - "... cannot convert from X to Y...".
  38. >So looks like compiler looks at method func2() in Derived
  39. >class.  I would think the signature of the call would make
  40. >it look in Base class.
  41.